package com.userfactssample.client.client.framework.formItems;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Element;
import com.smartgwt.mobile.client.types.Alignment;
import com.smartgwt.mobile.client.widgets.events.ClickEvent;
import com.smartgwt.mobile.client.widgets.events.ClickHandler;
import com.smartgwt.mobile.client.widgets.form.fields.FormItem;
import com.smartgwt.mobile.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.mobile.client.widgets.form.fields.events.ChangedHandler;
import com.userfactssample.client.client.framework.event.UMFormActionEvent;
import com.userfactssample.client.client.framework.handler.UMFormActionHandler;

public abstract class UMFormItem {
	private FormItem formItem;

	public UMFormItem(String title, FormItem formItem) {
		this(formItem);
		if (title != null)
			formItem.setTitle(title);
	}
	
	public UMFormItem(FormItem formItem) {
		super();
		this.formItem = formItem;
	}
	
	public FormItem getFormItem(){
		return formItem;
	}

	public void setName(String name) {
		this.formItem.setName(name);
	}
	
	public String getName() {
		return this.formItem.getName();
	}
	public void setTitle(String title) {
		this.formItem.setTitle(title);
		
	}
	
	public Boolean isRequired(){
		return this.formItem.isRequired();
	}
	
	public String getTitle(){
		return this.formItem.getTitle();
	}

	public void setRequired(boolean b) {
		this.formItem.setRequired(b);
		
	}
	
	public void setWidth(int i) {
		this.formItem.setWidth(i);
	}
	
	public void setWidth(String i) {
		this.formItem.setWidth(i);
	}
	
	public Object getValue(){
		if(formItem==null)
			return "";
		return this.formItem.getValue();
	}
	
	public void setValue(String value) {
		this.formItem.setValue(value);
	}
	
	public void setShowTitle(boolean b) {
		this.formItem.setShowTitle(b);
	}

	public Boolean validate() {
		return this.formItem.validate();
	}
	
	public HandlerRegistration addClickHandler(final UMFormActionHandler handler) {
		return formItem.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				UMFormActionEvent actionEvent = new UMFormActionEvent(UMFormItem.this);
				handler.onAction(actionEvent);
				
			}
		});
	}
	
	
	public HandlerRegistration addChangedHandler(final UMFormActionHandler handler) {
		
		return formItem.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				UMFormActionEvent actionEvent = new UMFormActionEvent(UMFormItem.this);
				handler.onAction(actionEvent);
				
			}
		});
		
	}

	public void clearValue() {
		try{
			this.formItem.clear();
		}
		catch(Exception ex){
			
		}
	}

	public void setValue(Object value) {
		this.formItem.setValue(value);
		
	}
	
	public void setValue(JavaScriptObject value) {
		this.formItem.setValue(value);
		
	}
	
	public void hide() {
		this.formItem.setVisible(false);
	}
	
	public void setHeight(int i) {
		this.formItem.setHeight(i);
		
	}
	
	public void setHeight(String s) {
		this.formItem.setHeight(s);
		
	}
	
	public void setAlign(Alignment align) {
		getElement().setAttribute("align",align.getValue());
		
	}
	
	private Element getElement() {
		return formItem.getElement();
	}

	public void setVisible(boolean b) {
		this.formItem.setVisible(b);
		
	}
	public void disable() {
		this.formItem.setDisabled(true);
	}
	
	public void enable() {
		this.formItem.setDisabled(false);
	}
	
	public void setReadOnly(Boolean readOnly) {
		getElement().setAttribute("readOnly", readOnly.toString());
	}
}